<?php
define('BONUS_MONEY_FOR_REF_USER',100);
module_load_include('inc', 'crf', 'crf');
function get_date_diff($earlierDate,$laterDate)
{
  $nTotalDiff = $laterDate - $earlierDate;
  $oDiff = array();

  $oDiff['days'] = floor($nTotalDiff/86400);
  $nTotalDiff -= $oDiff['days']*86400;

  $oDiff['hours'] = floor($nTotalDiff/3600);
  $nTotalDiff -= $oDiff['hours']*3600;

  $oDiff['minutes'] = floor($nTotalDiff/60);
  $nTotalDiff -= $oDiff['minutes']*60;

  $oDiff['seconds'] = $nTotalDiff;

  return $oDiff;
}
function update_user_point($uid,$product_id){
  $add_more =0;
  $sql = "SELECT COUNT(*) FROM {user_ext} ref JOIN {ltod_register} reg ON ref.uid = reg.user_id WHERE (ref.ref_by_1 = %d OR ref.ref_by_2 = %d OR ref.ref_by_3 = %d) AND reg.product_id =%d AND status=1";
  $direct_ref = db_result(db_query($sql, $uid,$uid,$uid,$product_id));
  if( $direct_ref){
    $add_more = $add_more  + $direct_ref *100;
  }
  $sql = "UPDATE {user_ext} SET money=money + %d WHERE  uid= %d";
  if(FALSE == db_query($sql,$add_more,$uid)){
    watchdog('tlod',t("Can't update money to database!"),array(),WATCHDOG_ERROR);
  }
  $temp_uid = $uid;
  for ( $i=0;$i < 3 && $temp_uid ; $i ++){
    $ref_user = db_result(db_query("SELECT ref_by_1 FROM {user_ext} WHERE uid=%d",$temp_uid));
    if($ref_user){
      db_query("UPDATE {user_ext} SET money=money + %d WHERE uid=%d", 100,$ref_user);
      $temp_uid = $ref_user;
    }else{
      $temp_uid =NULL;
    }
  }
}
function get_ltod_product($productid){
  return db_fetch_object(db_query('SELECT * FROM {ltod_product} WHERE product_id = %d', $productid));
}
/**
check product could buy or not
return a array
first element is result code: 0 is OK, less than 0  is Error
second element is detail error
*/
function check_product_can_buy($productid,$uid,$method){
  if( !user_access('register deals')){
    return array(-6,t('You must login( or have right permission) before buy this product'));
  }
  $product = get_ltod_product($productid);
  if($product){
    if($product->end_date<time()){
      return array(-1,t("This product is sold out!"));
    }else if($product->start_date>time()){
      return array(-2,t("This product is not ready!"));
    }
    if($method ==0){
      return array(0,$product);
    }else if($method ==1){
      $uset_ext = get_current_ext_info($uid);
      $uset_ext->money = $uset_ext->money?$uset_ext->money:0;
      $product->price=$product->price?$product->price:variable_get('min_buy_money',MIN_BUY_MONEY);
      if(!$uset_ext->money || $uset_ext->money < $product->price){
        return array(-4,t("Not enought money!Your money: @money",array('@money'=>$uset_ext->money)));
      }
    }else {
      $uset_ext = get_current_ext_info($uid);
      $uset_ext->bonus_money =$uset_ext->bonus_money?$uset_ext->bonus_money:0;
      $product->price=$product->price?$product->price:variable_get('min_buy_money',MIN_BUY_MONEY);
      if(!$uset_ext->bonus_money||$uset_ext->bonus_money < $product->price){
        return array(-5,t("Not enought money!Your left money: @money",array('@money'=>$uset_ext->bonus_money)));
      }
    }
  }else{
    return array(-3,t("Can't find this product"));
  }
  return array(0,$product);
}
/**
Save the registration of buying product
return a array
first element is result code: 0 is OK, less than 0  is Error
second element is detail error
*/
function save_register_deal($product_id,$user_id,$status){
  $secret_key = user_password(20);
  $query = 'SELECT * FROM {ltod_register} WHERE user_id=%d AND product_id=%d';
  $q = db_query($query,$user_id,$product_id);
  if ($r = db_fetch_array($q)) {
    //co du lieu va status =0 cap nhat lai secrect key
    if($r['status']==0){
      $query = "UPDATE {ltod_register} SET secret='%s' WHERE user_id=%d AND product_id=%d";
      if(FALSE===db_query($query,$secret_key,$user_id,$product_id)){
        return array(-1,t("Error system. Please try again!"));
      }
    }else{
      //thong bao loi
      return array(-1,t("You maybe bought or voted for this product. Can't buy one more time!"));
    }
  }else{
    //khong co du lieu
    $query = "INSERT INTO {ltod_register}
                        (user_id, status, secret, product_id)
                        VALUES (%d,%d,'%s',%d)";
    if( FALSE ===db_query($query,$user_id,$status,$secret_key,$product_id)){
      return array(-1,t("Error system. Please try again!"));
    }
  }
}
?>